﻿--[[
   @brief    ui logic for update_tips ui
   @author   hqzheng
   @version  1.0
   @date     2017-06-01
--]]

update_tips_view = {

    root_id = 0;
    dialog_ptr = nil;
    need_download_ctn_id = 0;
    downloading_ctn_id = 0;
    finish_download_ctn_id = 0;
    progress_ptr = nil;
    need_download_tips_id = 0;

    detected_tips = "";
    updating_tips = "";
    current_status = 0;

    show = function(cur_status)
        if update_tips_view.dialog_ptr == nil then
            update_tips_view.create_dialog();
        end;
        
        if update_tips_view.dialog_ptr ~= nil then
            update_tips_view.switch_to_status(PREDOWNLOAD_STATE.IDLE);
            update_tips_view.dialog_ptr:Restore();
            update_tips_view.set_defualt_version_desp();
            update_tips_view.dialog_ptr:Show(TRUE);
            update_tips_view.dialog_ptr:BringToTop();
        end;
    end;

    minimize = function(uilib, dwID_Click)
        if nil ~= update_tips_view.dialog_ptr then
            update_tips_view.dialog_ptr:Minimize();
        end
    end;

    create_dialog = function()
        local main_uilib = ui_utility.uilib();
        update_tips_view.root_id = layout_mgr.restore_layout("wegame_sys_menu\\WeGameUpdateDialog.tpf/TenioUILayout/UIObject");
        if update_tips_view.root_id ~= 0 then         
            update_tips_view.dialog_ptr = ui_utility.get_ctrl_ptr_by_id(update_tips_view.root_id);
            update_tips_view.downloading_ctn_id = ui_utility.get_ctrl_id_by_path(update_tips_view.root_id, "InnerCtn\\Ctn_Downloading");  
            update_tips_view.finish_download_ctn_id = ui_utility.get_ctrl_id_by_path(update_tips_view.root_id, "InnerCtn\\Ctn_FinishDownload");    
            update_tips_view.need_download_ctn_id = ui_utility.get_ctrl_id_by_path(update_tips_view.root_id, "InnerCtn\\Ctn_NeedDownload");
            update_tips_view.progress_ptr = ui_utility.get_ctrl_ptr_by_path(update_tips_view.root_id, "InnerCtn\\Ctn_Downloading\\Ctn_ProgressBg\\Level_Progress");
            update_tips_view.need_download_tips_id = ui_utility.get_ctrl_id_by_path(update_tips_view.need_download_ctn_id, "NeedDownloadTips");
        else
            base_utility.log("create update_tips_view dialog error!");
        end;
    end;

    close = function(uilib, dwControlId)
        layout_mgr.delay_remove_ctrl(update_tips_view.root_id);
        update_tips_view.root_id = 0;
        update_tips_view.dialog_ptr = nil;
        update_tips_view.progress_ptr = nil;
        update_tips_control.report_qos(23);
        -- 下载失败则重置其状态为空闲状态，让下次打开可以重试
        local resutl_tb = base_utility.json_to_table(update_tips_control.result)
        if nil ~= resutl_tb and 0 ~= resutl_tb.result then
             update_tips_control.state = PREDOWNLOAD_STATE.IDLE;
        end
    end;

    -- 更新tips内容
    update_tips_content = function (data)
        if nil == update_tips_view.dialog_ptr then
            return;
        end

        --设置版本信息
        update_tips_view.set_version_desp(data.ver_info, data.ver);
    end;

    switch_to_status = function (cur_status)
        if nil == update_tips_view.dialog_ptr then
            return;
        end;
        if cur_status == PREDOWNLOAD_STATE.IDLE then
            ui_utility.setproperty_by_id(update_tips_view.need_download_ctn_id, "Visible", "true");
            ui_utility.setproperty_by_id(update_tips_view.need_download_tips_id, "Caption", LoadStr("WG_UPDATE_NEED_UPDATE"));
            ui_utility.setproperty_by_id(update_tips_view.downloading_ctn_id, "Visible", "false");
            ui_utility.setproperty_by_id(update_tips_view.finish_download_ctn_id, "Visible", "false");
        elseif cur_status == PREDOWNLOAD_STATE.DOWNLOADING then
            ui_utility.setproperty_by_id(update_tips_view.need_download_ctn_id, "Visible", "false");
            ui_utility.setproperty_by_id(update_tips_view.downloading_ctn_id, "Visible", "true");
            ui_utility.setproperty_by_id(update_tips_view.finish_download_ctn_id, "Visible", "false");
        elseif cur_status == PREDOWNLOAD_STATE.DOWNLOAD_END or 
            cur_status == PREDOWNLOAD_STATE.DOWNLOAD_RECOMMEND_PKG_END then
            ui_utility.setproperty_by_id(update_tips_view.need_download_ctn_id, "Visible", "false");
            ui_utility.setproperty_by_id(update_tips_view.downloading_ctn_id, "Visible", "false");
            ui_utility.setproperty_by_id(update_tips_view.finish_download_ctn_id, "Visible", "true");
        end;

        if update_tips_view.current_status ~= cur_status then
            update_tips_view.current_status = cur_status;
            update_tips_view.refresh_version_desp();
        end;
    end;

    -- 更新进度
    update_progress = function (data)
        if nil == update_tips_view.dialog_ptr then
            return;
        end
        local update_pace_id = ui_utility.get_ctrl_id_by_path(update_tips_view.downloading_ctn_id, "DonwloadSpeed");
        local update_tips_id = ui_utility.get_ctrl_id_by_path(update_tips_view.downloading_ctn_id, "DonwloadingTips");
        local update_percent_id = ui_utility.get_ctrl_id_by_path(update_tips_view.downloading_ctn_id, "DonwloadPercent");
        local update_tips = "";
        if 0 == data.step then
            ui_utility.setproperty_by_id(update_pace_id, "Visible", "false");
            ui_utility.setproperty_by_id(update_percent_id, "Visible", "false");
            update_tips = LoadStr("WG_UPDATE_PROGRESS_CHECK");
            ui_utility.setproperty_by_id(update_tips_id, "Caption", update_tips);
        elseif 1 == data.step then
            ui_utility.setproperty_by_id(update_pace_id, "Visible", "true");
            local speed = string.format("%s KB/s", data.speed);
            ui_utility.setproperty_by_id(update_pace_id, "Caption", speed);
            local downloaded_size = math.floor(data.downloaded_size/(1024*1024) * 10 + 0.5) / 10;
            local total_size = math.floor(data.total_size/(1024*1024) * 10 + 0.5) / 10;
            update_tips = LoadStr("WG_UPDATE_PROGRESS_DOWNLOAD");
            update_percent = tostring(data.progress) .. "%";
            ui_utility.setproperty_by_id(update_tips_id, "Caption", update_tips);
            ui_utility.setproperty_by_id(update_percent_id, "Caption", update_percent);
        else
            ui_utility.setproperty_by_id(update_pace_id, "Visible", "false");
            ui_utility.setproperty_by_id(update_percent_id, "Visible", "false");
            ui_utility.setproperty_by_id(update_tips_id, "Caption", update_tips);
        end

        -- 设置进度条
        if data.progress > 100 then
            data.progress = 100;
        end

        if nil ~= update_tips_view.progress_ptr then
            update_tips_view.progress_ptr:SetPos(data.progress, TRUE);
        end;
    end;

    -- 更新结果
    update_result = function (data)
        if nil == update_tips_view.dialog_ptr then
            return;
        end
        local restart_btn_id = ui_utility.get_ctrl_id_by_path(update_tips_view.finish_download_ctn_id, "Btn_Restart");
        local cancle_btn_id = ui_utility.get_ctrl_id_by_path(update_tips_view.finish_download_ctn_id, "Btn_Cancle");
        local tips_id = ui_utility.get_ctrl_id_by_path(update_tips_view.finish_download_ctn_id, "FinishDownloadTips");
        if 0 == data.result then
            if nil ~= update_tips_view.progress_ptr then
                update_tips_view.progress_ptr:SetPos(data.progress, TRUE);
            end;
            ui_utility.setproperty_by_id(restart_btn_id, "Visible", "true");
            ui_utility.setproperty_by_id(cancle_btn_id, "Visible", "true");
            ui_utility.setproperty_by_id(tips_id, "Caption", LoadStr("WG_UPDATE_RESTART"));
        else
            ui_utility.setproperty_by_id(restart_btn_id, "Visible", "false");
            ui_utility.setproperty_by_id(cancle_btn_id, "Visible", "false");
            ui_utility.setproperty_by_id(tips_id, "Caption", LoadStr("WG_UPDATE_DOWNLOAD_FAILED"));
        end
    end;


    -- 设置默认版本特性说明
    set_defualt_version_desp = function ()
        if nil == update_tips_view.dialog_ptr then
            return;
        end
        local ver_str = "";

        if nil ~= update_tips_control.recommend_version and "" ~= update_tips_control.recommend_version then
            ver_str = update_tips_control.recommend_version;
        else
            local cur_ver_str = NewStr(260);
            local game_lib = base_utility.get_component_adapter("IGame_library",
                                "ierd_tgp.game_library.Get_game_library_adapter();");
            if nil ~= game_lib then
                local is_dev_ver = game_lib:get_tgp_version(cur_ver_str, 260, false);
                ver_str = CUIStrtoU8(U8toCUIStr(cur_ver_str));
            end
        end;
        local version_info_1 = LoadStr("WG_UPDATE_VERSION_TIP1");
        local version_info_2 = LoadStr("WG_UPDATE_VERSION_TIP2");
        local default_version_info = string.format("1.%s<b>2.%s", version_info_1, version_info_2);
        update_tips_view.set_version_desp(default_version_info, ver_str);
    end;

    -- 设置版本特性说明
    set_version_desp = function(sJson, sVer)
        if nil == sJson or "" == sJson then
            return;
        end
        --[[if nil == update_tips_view.dialog_ptr then
            return;
        end
        local pCtrl = ui_utility.get_ctrl_ptr_by_path(update_tips_view.root_id,
                            "InnerCtn\\Ctn_NeedUpdate\\svDesp\\edtDesp");
        local interface = "Tenio::IRichTextBox";
        local pEdit = tolua.cast(pCtrl, interface);

        if nil ~= pEdit then
            local need_tips = string.format(LoadStr("WEGAME_SYS_MENU_VERSION_UPDATING_TIPS"), sVer);
            local sTips = need_tips.."<b>"..sJson;
            local sText = U8toCUIStr(sTips);

            pEdit:Clear();
            pEdit:PushBackHtmlString(sText);
            local pView = ui_utility.get_ctrl_ptr_by_path(update_tips_view.root_id,
                             "InnerCtn\\Ctn_NeedUpdate\\svDesp");
            if nil ~= pView then
                pView:SetVScrollPosToStart();
            end;
        end;]]--
        local detected_tips = string.format(LoadStr("WEGAME_SYS_MENU_VERSION_DETECT_NEW_TIPS"), sVer);
        local updating_tips = string.format(LoadStr("WEGAME_SYS_MENU_VERSION_UPDATING_TIPS"), sVer);
        update_tips_view.detected_tips = detected_tips.."<b>"..sJson;
        update_tips_view.updating_tips = updating_tips.."<b>"..sJson;
        update_tips_view.refresh_version_desp(true);
    end;

    refresh_version_desp = function(scroll_to_start)
        local tips_info = update_tips_view.updating_tips;
        if update_tips_view.current_status ~= nil and
           update_tips_view.current_status == PREDOWNLOAD_STATE.IDLE then
            tips_info = update_tips_view.detected_tips;
        end;

        if tips_info == nil or tips_info == "" then
            return;
        end;

        if nil == update_tips_view.dialog_ptr then
            return;
        end
        local pCtrl = ui_utility.get_ctrl_ptr_by_path(update_tips_view.root_id,
                            "InnerCtn\\Ctn_NeedUpdate\\svDesp\\edtDesp");
        local interface = "Tenio::IRichTextBox";
        local pEdit = tolua.cast(pCtrl, interface);

        if nil ~= pEdit then
            --local need_tips = string.format(LoadStr("WEGAME_SYS_MENU_VERSION_UPDATING_TIPS"), sVer);
            --local sTips = need_tips.."<b>"..sJson;
            local sText = U8toCUIStr(tips_info);

            pEdit:Clear();
            pEdit:PushBackHtmlString(sText);
            local pView = ui_utility.get_ctrl_ptr_by_path(update_tips_view.root_id,
                             "InnerCtn\\Ctn_NeedUpdate\\svDesp");
            if nil ~= pView and scroll_to_start ~= nil and scroll_to_start == true then
                pView:SetVScrollPosToStart();
            end;
        end;
    end;

    on_restart_app = function (uilib, control_id)
        -- 重启
        update_tips_control.report_qos(22);
        update_dialog_control.do_update_app();
    end;

    on_download = function (uilib, control_id)
        update_tips_control.start_predownload();
    end;

    set_update_tips = function (tips)
        ui_utility.setproperty_by_id(update_tips_view.need_download_tips_id, "Caption", tips);
    end;

};